package com.thon.controller.rest;

import com.google.common.collect.Lists;
import com.thon.commons.persistence.Page;
import com.thon.commons.utils.StringUtils;
import com.thon.controller.util.BaseController;
import com.thon.entity.college.*;
import com.thon.service.college.CollegeService;
import com.thon.service.college.CtermService;
import com.thon.service.college.MajorService;
import com.thon.service.college.ScoreService;
import com.thon.service.utils.UserUtils;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by win on 2015/12/21.
 */
@Controller
@RequestMapping("/api/college")
public class
CollegeRestController extends BaseController {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private CtermService ctermService;



    //添加/修改大学
    @RequestMapping(value="/save", method = RequestMethod.POST)
    public ResponseEntity<?> apiCollegeAdd(@Valid College college,
                                           @RequestParam(value = "majorIds",required = false) Integer[] majorIds,
                                           @RequestParam(value = "termIds",required = false) Integer[] termIds) {
        List<Major> majors = Lists.newArrayList();
        //添加专业List
        if(majorIds!=null){
            for (int id : majorIds) {
                Major major = new Major();
                major.setId(id);
                majors.add(major);
            }
            college.setMajorList(majors);
        }
        if(termIds!=null){
            List<Cterm> cterms = Lists.newArrayList();
            for (int id : termIds) {
                Cterm cterm = new Cterm();
                cterm.setId(id);
                cterms.add(cterm);
            }
            college.setCtermList(cterms);
        }
        //添加标签List
        collegeService.saveCollege(college);

        return new ResponseEntity(true, HttpStatus.OK);
    }


    //删除大学
    @RequestMapping(value="/del", method = RequestMethod.POST)
    public ResponseEntity<?> apiCollegeDel(@RequestParam("id") int id) {
        if(UserUtils.getUser().isAdmin()){
            College college = collegeService.getCollege(id);
            collegeService.deleteCollege(college);
            return new ResponseEntity("true", HttpStatus.OK);
        }else {
            return new ResponseEntity("false", HttpStatus.FORBIDDEN);
        }
    }

    //分页+条件查询所有大学
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<?> list(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                  @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                  @RequestParam(value = "ctermNames", required = false) String ctermNames,
                                  @RequestParam(value = "orderName", defaultValue = "id") String orderName,
                                  @RequestParam(value = "order", defaultValue = "desc") String order,
                                  College college) {

        Page<College> page = new Page<College>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = collegeService.findColleges(page, college,ctermNames, orderName, order);

        return new ResponseEntity(page, HttpStatus.OK);
    }

    //大学排行榜
    @RequestMapping(value = "/listRank", method = RequestMethod.GET)
    public ResponseEntity<?> collegeRankList(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                       @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                       @RequestParam(value = "rank", defaultValue = PAGE_SIZE) int rank,
                                       College college) {

        Page<College> page = new Page<College>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = collegeService.findRankColleges(page, college,rank);

        return new ResponseEntity(page, HttpStatus.OK);
    }


    //分页+条件查所有专业
    @RequestMapping(value = "/major/list", method = RequestMethod.GET)
    public ResponseEntity<?> getList(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                     @RequestParam(value = "collegeNames", defaultValue = PAGE_SIZE) String collegeNames,
                                     Major major) {
        Page<Major> page = new Page<Major>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = collegeService.findCollegeMajors(page, major,collegeNames);

        return new ResponseEntity(page, HttpStatus.OK);
    }

    //专业排行榜
    @RequestMapping(value = "/major/listRank", method = RequestMethod.GET)
    public ResponseEntity<?> majorRankList(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                     @RequestParam(value = "rank", defaultValue = PAGE_SIZE) int rank,
                                     Major major) {
        Page<Major> page = new Page<Major>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = collegeService.findRankMajors(page, major, rank);

        return new ResponseEntity(page, HttpStatus.OK);
    }

    //根据地区parentId查询它对应的所有子专业
    @RequestMapping(value = "/major/list/parent", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<HttpEntity> listRegions(
            @RequestParam(value = "parent.id", defaultValue="10") int parentId,
            Model model) {

        List<Major> majors = majorService.listMajors(parentId);
        return new ResponseEntity(majors, HttpStatus.OK);
    }

    //添加/修改专业
    @RequestMapping(value = "/major/save", method = RequestMethod.POST)
    public ResponseEntity<?> saveMajor(@Valid Major major){
        collegeService.saveMajor(major);
        return new ResponseEntity("true",HttpStatus.OK);
    }

    //删除专业
    @RequestMapping(value = "/major/del", method = RequestMethod.POST)
    public ResponseEntity<?> deleteMajor(@RequestParam(value = "id",required = true)int id){
        if(UserUtils.getUser().isAdmin()){
            Major major = majorService.getMajor(id);
            majorService.deleteMajor(major);
            return new ResponseEntity("true", HttpStatus.OK);
        }else {
            return new ResponseEntity("false", HttpStatus.FORBIDDEN);
        }
    }

    //添加/修改分数线
    @RequestMapping(value="/score/save", method = RequestMethod.POST)
    public ResponseEntity<?> apiScoreAdd(@Valid Score score) {

        if( score.getMajor() == null || score.getMajor().getId() == null){
            score.setMajor(null);
        }

        if(score.getCollege().getId() == null){
            score.setCollege(null);
        }

        scoreService.saveScore(score);
        return new ResponseEntity(true, HttpStatus.OK);
    }

    //删除分数线
    @RequestMapping(value="/score/del", method = RequestMethod.POST)
    public ResponseEntity<?> apiMajorDel(@RequestParam("id") int id) {
        if(UserUtils.getUser().isAdmin()){
            Score score = scoreService.getScore(id);
            scoreService.deleteScore(score);
            return new ResponseEntity("true", HttpStatus.OK);
        }else {
            return new ResponseEntity("false", HttpStatus.FORBIDDEN);
        }
    }

    //分页+条件查询所有分数线
    @RequestMapping(value = "/score/list", method = RequestMethod.GET)
    public ResponseEntity<?> list(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                  @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                  @RequestParam(value = "orderName", defaultValue = "id") String orderName,
                                  @RequestParam(value = "order", defaultValue = "desc") String order,
                                  Score score) {

        Page<Score> page = new Page<Score>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = scoreService.findScores(page,score, orderName, order);

        return new ResponseEntity(page, HttpStatus.OK);
    }

    //查询所有大学标签
    @RequestMapping(value = "/cterm/list", method = RequestMethod.GET)
    public ResponseEntity<?> getList(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                     Cterm cterm) {
        Page<Cterm> page = new Page<Cterm>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = ctermService.findCterms(page, cterm);
        return new ResponseEntity(page, HttpStatus.OK);
    }

    //删除大学标签
    @RequestMapping(value = "/cterm/del", method = RequestMethod.POST)
    public ResponseEntity<?> deleteTerm(@RequestParam(value = "id",required = true)int id){
        Cterm cterm = ctermService.getCterm(id);
        ctermService.deleteCterm(cterm);
        return new ResponseEntity("true", HttpStatus.OK);
    }

    //保存/修改大学标签
    @RequestMapping(value = "/cterm/save", method = RequestMethod.POST)
    public ResponseEntity<?> saveTerm(@Valid Cterm cterm){
        ctermService.saveCterm(cterm);
        return new ResponseEntity("true",HttpStatus.OK);
    }

    //查询所有jobCity
    @RequestMapping(value = "/major/jobCity/list", method = RequestMethod.GET)
    public ResponseEntity<?> getJobCityList(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                     JobCity jobCity) {
        Page<JobCity> page = new Page<JobCity>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = majorService.findJobCities(page, jobCity);
        return new ResponseEntity(page, HttpStatus.OK);
    }

    //删除jobCity
    @RequestMapping(value = "/major/jobCity/del", method = RequestMethod.POST)
    public ResponseEntity<?> deleteJobCity(@RequestParam(value = "id",required = true)int id){
        JobCity jobCity = majorService.getJobCity(id);
        majorService.deleteJobCity(jobCity);
        return new ResponseEntity("true", HttpStatus.OK);
    }

    //保存/修改jobCity
    @RequestMapping(value = "/major/jobCity/save", method = RequestMethod.POST)
    public ResponseEntity<?> saveJobCity(@Valid JobCity jobCity){
        majorService.saveJobCity(jobCity);
        return new ResponseEntity("true",HttpStatus.OK);
    }


    //添加大学LOGO
    @RequestMapping(value="/profile/avatar/update", method = RequestMethod.POST)
    public ResponseEntity<?> apiProfileAvatarUpdate(HttpServletRequest request) {

            Integer collegeId = Integer.valueOf(request.getParameter("id"));
            Integer avatar = Integer.valueOf(request.getParameter("collegeLogo"));

            College college = collegeService.getCollege(collegeId);
            college.setCollegeLogo(avatar);
            collegeService.saveCollege(college);


        return new ResponseEntity(true, HttpStatus.OK);
    }


    //查询所有examStudent
    @RequestMapping(value = "/score/examStudent/list", method = RequestMethod.GET)
    public ResponseEntity<?> getExamStudentList(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                            @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                            ExamStudent examStudent) {
        Page<ExamStudent> page = new Page<ExamStudent>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = scoreService.findExamStudents(page, examStudent);
        return new ResponseEntity(page, HttpStatus.OK);
    }

    //删除examStudent
    @RequestMapping(value = "/score/examStudent/del", method = RequestMethod.POST)
    public ResponseEntity<?> deleteExamStudent(@RequestParam(value = "id",required = true)int id){
        ExamStudent examStudent = scoreService.getExamStudent(id);
        scoreService.deleteExamStudent(examStudent);
        return new ResponseEntity("true", HttpStatus.OK);
    }

    //保存/修改examStudent
    @RequestMapping(value = "/score/examStudent/save", method = RequestMethod.POST)
    public ResponseEntity<?> saveExamStudent(@Valid ExamStudent examStudent){
        scoreService.saveExamStudent(examStudent);
        return new ResponseEntity("true",HttpStatus.OK);
    }

    //验证学校名称是否重复
    @RequestMapping(value = "/collegeName/check", method = RequestMethod.GET)
    public ResponseEntity<?> apiCollegeNameCheck(@RequestParam("collegeName") String collegeName,
                                                 @RequestParam(value = "collegeId", defaultValue = "0") int collegeId) {
        College college = collegeService.findCollegeByName(collegeName, null);
        if ( college == null || college.getId().equals(collegeId)) {
            return new ResponseEntity("true", HttpStatus.OK);
        } else {
            return new ResponseEntity("false", HttpStatus.OK);
        }
    }

    //验证专业名称是否重复
    @RequestMapping(value = "/major/majorName/check", method = RequestMethod.GET)
    public ResponseEntity<?> apiMajorNameCheck(@RequestParam("majorName") String majorName,
                                                 @RequestParam(value = "majorId", defaultValue = "0") int majorId) {
        Major major = collegeService.findMajorByName(majorName, null);
        if ( major == null || major.getId().equals(majorId)) {
            return new ResponseEntity("true", HttpStatus.OK);
        } else {
            return new ResponseEntity("false", HttpStatus.OK);
        }
    }


    //添加/修改大学图片
    @RequestMapping(value="/collegeImage/save", method = RequestMethod.POST)
    public ResponseEntity<?> apiCollegeImageAdd(@Valid CollegeImage collegeImage) {

        collegeService.saveCollegeImage(collegeImage);
        return new ResponseEntity(true, HttpStatus.OK);
    }

    //删除大学图片
    @RequestMapping(value="/collegeImage/del", method = RequestMethod.POST)
    public ResponseEntity<?> apiCollegeImageDel(@RequestParam("id") int id) {
        if(UserUtils.getUser().isAdmin()){
            CollegeImage collegeImage = collegeService.getCollegeImage(id);
            collegeService.deleteCollegeImage(collegeImage);
            return new ResponseEntity("true", HttpStatus.OK);
        }else {
            return new ResponseEntity("false", HttpStatus.FORBIDDEN);
        }
    }

    //分页+条件查询所有大学图片
    @RequestMapping(value = "/collegeImage/list", method = RequestMethod.GET)
    public ResponseEntity<?> listCollegeImage(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                  @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                  @RequestParam(value = "orderName", defaultValue = "id") String orderName,
                                  @RequestParam(value = "order", defaultValue = "desc") String order,
                                  CollegeImage collegeImage) {

        Page<CollegeImage> page = new Page<CollegeImage>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = collegeService.findCollegeImages(page,collegeImage, orderName, order);

        return new ResponseEntity(page, HttpStatus.OK);
    }


}